package org.hismig.migration.parambuilder;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.hismig.cfg.ParamBuilder;
import org.hismig.migration.Context;
import org.hismig.utils.DateTool;
import org.hismig.utils.StringTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-07-04 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class NetureDayBuilder implements IParamBuilder {

	private ParamBuilder paramBuilder; // cfg
	private SimpleDateFormat simpleDateFormat;

	private static final String KEY_PATTERN = "pattern";
	private static final String KEY_SCOPE = "scope";

	@Override
	public void paramBuild(Context context) {

		String forName = paramBuilder.getForName();

		if (null == simpleDateFormat) {
			String pattern = paramBuilder.getParamByName(KEY_PATTERN);

			if (null == pattern || pattern.isEmpty()) {
				simpleDateFormat = DateTool.getNormalDateSDF();
			} else {
				simpleDateFormat = new SimpleDateFormat(pattern);
			}

		}

		int scope = StringTool.getIntFormString(paramBuilder
				.getParamByName(KEY_SCOPE));

		Date theDay = DateTool.CountDate(scope);

		context.set(forName, simpleDateFormat.format(theDay));
	}

	@Override
	public void setParamBuilder(ParamBuilder paramBuilder) {
		this.paramBuilder = paramBuilder;
	}

}
